/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.showcase.component.page.old;

import java.lang.reflect.Method;

import com.google.code.swingonfire.component.page.old.JPageAnimationFactory;
import com.google.code.swingonfire.component.page.old.JPageCompositeAnimationFactory;
import com.google.code.swingonfire.component.page.old.JPageTransitionAnimationFactory;
import com.google.code.swingonfire.utility.Reflection;

public enum OldJPagePaneSamples
{
    NONE("None", "No animation", null, null, null),
    FADE_IN("Fade In", "A simple fade operation", JPageTransitionAnimationFactory.class, "fadeIn", "fadeIn"),
    FADE_IN_AND_SHRINK("Fade In With Shrinking", "A fade operation with some shrinking",
        JPageTransitionAnimationFactory.class, "fadeInAndShrink", "fadeInAndShrink"),
    FADE_IN_HORIZONTAL("Horizontal Fade In", "Moves and fades the next image in horizontally",
        JPageTransitionAnimationFactory.class, "fadeInFromLeft", "fadeInFromRight"),
    FADE_IN_VERTICAL("Vertical Fade In", "Moves and fades the next image in vertically",
        JPageTransitionAnimationFactory.class, "fadeInFromTop", "fadeInFromBottom"),
    FADE_IN_HORIZONTAL_AND_SHRINK("Horizontal Fade In With Shrinking",
        "Moves, fades and shrinks the next image in horizontally", JPageTransitionAnimationFactory.class,
        "fadeInFromLeftAndShrink", "fadeInFromRightAndShrink"),
    FADE_IN_VERTICAL_AND_SHRINK("Vertical Fade In With Shrinking",
        "Moves, fades and shrinks the next image in vertically", JPageTransitionAnimationFactory.class,
        "fadeInFromTopAndShrink", "fadeInFromBottomAndShrink"),
    SEQUENTIAL_FADE("Sequential Fade", "A fade operation with a pause", JPageTransitionAnimationFactory.class,
        "sequentialFade", "sequentialFade"),
    MOVE_IN_HORIZONTAL("Horizontal Move In", "Moves the next image in and the last image out horizontally",
        JPageTransitionAnimationFactory.class, "moveInFromLeft", "moveInFromRight"),
    MOVE_IN_VERTICAL("Vertical Move In", "Moves the next image in and the last image out vertically",
        JPageTransitionAnimationFactory.class, "moveInFromTop", "moveInFromBottom"),
    NEWSPAPER("Newspaper", "Once a journalist was killed by a rotating newspaper",
        JPageTransitionAnimationFactory.class, "newspaperFromLeft", "newspaperFromRight"),
    HALF("Half", "lala", JPageCompositeAnimationFactory.class, "test", "test");

    private final String name;
    private final String description;
    private final Class<?> type;
    private final String methodForPrevious;
    private final String methodForNext;

    private OldJPagePaneSamples(String name, String description, Class<?> type, String methodForPrevious,
        String methodForNext)
    {
        this.name = name;
        this.description = description;
        this.type = type;
        this.methodForPrevious = methodForPrevious;
        this.methodForNext = methodForNext;
    }

    public String getName()
    {
        return name;
    }

    public String getDescription()
    {
        return description;
    }

    public Class<?> getType()
    {
        return type;
    }

    public String getMethodForPrevious()
    {
        return methodForPrevious;
    }

    public String getMethodForNext()
    {
        return methodForNext;
    }

    public JPageAnimationFactory getAnimationFactoryForPrevious(double duration)
    {
        return getAnimationFactory(methodForPrevious, duration);
    }

    public JPageAnimationFactory getAnimationFactoryForNext(double duration)
    {
        return getAnimationFactory(methodForNext, duration);
    }

    private JPageAnimationFactory getAnimationFactory(String name, double duration)
    {
        if (type == null)
        {
            return null;
        }

        Method method = Reflection.findMethod(type, name, double.class);

        if (method == null)
        {
            throw new IllegalArgumentException("Failed to find method: " + type + "." + name);
        }

        return (JPageAnimationFactory) Reflection.invokeMethod(method, null, duration);
    }

    public String getFullDescription()
    {
        StringBuilder builder = new StringBuilder();

        builder.append("// ").append(getDescription().replaceAll("\\n", "\n// "));
        builder.append("\n\n");

        builder.append("pagePane.setAnimationFactoryForPrevious(");

        if (type == null)
        {
            builder.append("null");
        }
        else
        {
            builder.append(type.getSimpleName()).append(".").append(methodForPrevious).append("(..)");
        }

        builder.append(");\n");

        builder.append("pagePane.setAnimationFactoryForNext(");

        if (type == null)
        {
            builder.append("null");
        }
        else
        {
            builder.append(type.getSimpleName()).append(".").append(methodForNext).append("(..)");
        }

        builder.append(");\n");

        return builder.toString();
    }

    @Override
    public String toString()
    {
        return name;
    }

}
